#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.IO;
using NUnit.Framework;
using Newtonsoft.Utilities.IO;
using System;

namespace Newtonsoft.Utilities.Tests.IO
{
  public class FileUtilsTests : TestsBase
  {
    [Test]
    public void MapPath_ReturnRootedPathUnchanged()
    {
      string rootedPath = @"c:\";

      Assert.AreEqual(rootedPath, FileUtils.MapPath(rootedPath));
    }

    [Test]
    [ExpectedException(typeof(Exception), ExpectedMessage = @"Could not resolve non-rooted path: .\")]
    public void MapPath_RelativePathShouldError()
    {
      FileUtils.MapPath(@".\");
    }

    [Test]
    public void ToValidFileName_RemoveInvalidCharacters()
    {
      string fileName = FileUtils.ToValidFileName(@"NAT retest 23/01/07 - 24 Jan 2007 - Version 1.pdf");

      Assert.AreEqual("NAT retest 230107 - 24 Jan 2007 - Version 1.pdf", fileName);
    }

    [Test]
    public void ToValidFileName_ReplaceInvalidCharacters()
    {
      string fileName = FileUtils.ToValidFileName(@"NAT retest 23/01/07 - 24 Jan 2007 - Version 1.pdf", "_");

      Assert.AreEqual("NAT retest 23_01_07 - 24 Jan 2007 - Version 1.pdf", fileName);
    }

    [Test]
    public void ToValidFileName_ReplaceSpacesWithUnderscores()
    {
      string fileName = FileUtils.ToValidFileName(@"NAT retest 23/01/07 - 24 Jan 2007 - Version 1.pdf", string.Empty, "_");

      Assert.AreEqual("NAT_retest_230107_-_24_Jan_2007_-_Version_1.pdf", fileName);
    }

    [Test]
    public void ToValidFileName_RemoveSpaces()
    {
      string fileName = FileUtils.ToValidFileName(@"NAT retest 23/01/07 - 24 Jan 2007 - Version 1.pdf", string.Empty, string.Empty);

      Assert.AreEqual("NATretest230107-24Jan2007-Version1.pdf", fileName);
    }

    [Test]
    public void GetFileSystemInfo_ShouldReturnDirectoryInfo()
    {
      string currentPath = FileUtils.MapPath("~/");
      string directoryPath = Path.Combine(currentPath, "GetFileSystemInfoDirectory");

      FileUtils.EnsureDirectoryExists(directoryPath);

      FileSystemInfo fileSystemInfo = FileUtils.GetFileSystemInfo(directoryPath);
      Assert.IsNotNull(fileSystemInfo);
      Assert.IsInstanceOfType(typeof(DirectoryInfo), fileSystemInfo);
    }

    [Test]
    public void GetFileSystemInfo_ShouldReturnFileInfo()
    {
      string currentPath = FileUtils.MapPath("~/");
      string filePath = Path.Combine(currentPath, "GetFileSystemInfoFile");

      FileUtils.EnsureFileExists(filePath);

      FileSystemInfo fileSystemInfo = FileUtils.GetFileSystemInfo(filePath);
      Assert.IsNotNull(fileSystemInfo);
      Assert.IsInstanceOfType(typeof(FileInfo), fileSystemInfo);
    }

    [Test]
    public void GetFileSystemInfo_ShouldReturnNull()
    {
      string currentPath = FileUtils.MapPath("~/");
      string filePath = Path.Combine(currentPath, "GetFileSystemInfoNothing");

      FileUtils.EnsureFileDeleted(filePath);
      FileUtils.EnsureDirectoryDeleted(filePath);

      FileSystemInfo fileSystemInfo = FileUtils.GetFileSystemInfo(filePath);
      Assert.IsNull(fileSystemInfo);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: path")]
    public void GetFileSystemInfo_ShouldErrorOnNullPath()
    {
      FileUtils.GetFileSystemInfo(null);
    }
  }
}